#include <signal.h>
#include <stdio.h>
#include "shell.h"

extern pid_t foreground_pgid;

void sigint_handler(int sig) {
    if (foreground_pgid != 0) {
        kill(-foreground_pgid, SIGINT);
    }
}

void sigchld_handler(int sig) {
    while (waitpid(-1, NULL, WNOHANG) > 0);
}

void setup_signal_handlers() {
    struct sigaction sa_int = { .sa_handler = sigint_handler };
    struct sigaction sa_chld = { .sa_handler = sigchld_handler };

    sigaction(SIGINT, &sa_int, NULL);
    sigaction(SIGCHLD, &sa_chld, NULL);
}
